﻿using NUnit.Core;
using PC.PickleJar.NUnit.Adapters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PC.PickleJar.NUnit
{
	public class TestMethodWrapper : TestMethod
	{
		public TestMethodWrapper(MethodInfo testMethodInfo)
			:base(testMethodInfo)
		{
		}

		//public override TestResult Run(EventListener listener, ITestFilter filter)
		//{
		//	TestResult result = this.ValidateTestMethod();

		//	if (result == null)
		//	{
		//		result = base.Run(listener, filter);
		//	}

		//	return result;
		//}

		public override TestResult RunTest()
		{
			TestResult result = this.ValidateTestMethod();

			if (result == null)
			{
				result = base.RunTest();
			}

			return result;
		}

		private TestResult ValidateTestMethod()
		{
			TestResult result = null;

			PickleJarEngine engine = new PickleJarEngine();
			var validationResult = engine.ValidateTestMethod(base.Method);

			if (validationResult.Result == ResultEnum.Failure)
			{
				ValidationResultActionAdapter adapter = new ValidationResultActionAdapter();
				result = new TestResult(this);
				result.SetResult(
					adapter.GetResultState(validationResult.Action),
					validationResult.Message,
					Environment.StackTrace);
			}

			return result;
		}
	}
}
